package com.jt.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jt.mapper.RoleMapper;
import com.jt.pojo.Menu;
import com.jt.pojo.Result;
import com.jt.pojo.Role;

import com.jt.pojo.RoleMenu;
import com.jt.service.RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public Role selectById(String roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public List<String> selectRouterIdListByRoleId(String roleId) {
        return Collections.emptyList();
    }


    @Override
    public List<Role> getRoleList() {
        List<Role> roleList = roleMapper.getRoleList();
        roleList.forEach(role -> {
            String permissionsStr = role.getPermissionsStr();
            if (permissionsStr != null) {
                role.setPermissions(Arrays.asList(permissionsStr.split(",")));
            }else {
                role.setPermissions(Collections.emptyList());
            }
        });
        return roleList;
    }

    @Override
    public boolean insertRole(Role role) {
        return roleMapper.insert(role) > 0 ? true : false;
    }

    @Override
    public Result selectMenuListByRoleId(String roleId) {
        List<String> menuIds = roleMapper.selectMenuListByRoleId(roleId);
        List<Menu> menuList = roleMapper.selectAllMenuList();
        List<Menu> tree = getTree(menuList, menuIds);
        return Result.success(tree);
    }

    @Override
    public Result saveRoleMenus(String roleId, List<String> menuList) {
        roleMapper.deleteRoleMenuRoleId(roleId);
        List<RoleMenu> roleMenuList = menuList.stream().map(menuId -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenu.setId(UUID.randomUUID().toString().substring(6));
            return roleMenu;
        }).collect(Collectors.toList());
        boolean  b = roleMapper.insertRoleMenuList(roleMenuList);
        return Result.success(b?"保存权限成功": "保存权限失败");
    }

    private List<Menu> getTree(List<Menu> menuList, List<String> menuIds) {
        List<Menu> oneMenu = menuList.stream().filter(a -> a.getParent().equals("0")).collect(Collectors.toList());
        setSon(menuList, oneMenu, menuIds);
        return oneMenu;
    }

    private void setSon(List<Menu> menuList, List<Menu> oneMenu, List<String> menuIds) {
        oneMenu.forEach(one -> {
            List<Menu> children = menuList.stream().filter(item -> item.getParent().equals(one.getId())).collect(Collectors.toList());
            if (menuIds.contains(one.getId())) {
                one.setRoleShow(true);
            }
            if (!CollectionUtils.isEmpty(children)) {
                one.setChildren(children);
                setSon(menuList, children, menuIds);
            }
        });
    }

}
